package com.tsd.core.vo;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * 接口响应类
 * 包括错误编码、信息、实际数据，对于分页查询，则返回分页信息
 *
 * @param <T>
 * @author wudw
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class HttpResult<T> implements Serializable {
    private static final long serialVersionUID = -1948042386850953730L;

    /**
     * 错误编码
     */
    Integer code = null;
    /**
     * 错误信息
     */
    String message = null;
    /**
     * 实际数据
     */
    T data = null;
    /**
     * 分页信息，对于查询列表时有效
     */
    PageBean page = null;
    /**
     * 详细的异常跟踪信息
     */
    String trace;

    /**
     * 参数Map列表，只是用于测试使用:key-paramName,value-param的说明
     */
//	Map<String,String> params = null;

    Object extra = null;

    public HttpResult() {
        this.code = ErrorCode.CODE_SUCCESS;
    }

    public HttpResult(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public HttpResult(Integer code, String msg, T data, PageBean page) {
        this.code = code;
        this.message = msg;
        this.data = data;
        this.page = page;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public PageBean getPage() {
        return page;
    }

    public void setPage(PageBean page) {
        this.page = page;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTrace() {
        return trace;
    }

    public void setTrace(String trace) {
        this.trace = trace;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    public static <T> HttpResult<T> success() {
        HttpResult<T> result = new HttpResult<>();
        result.setCode(ErrorCode.CODE_SUCCESS);
        return result;
    }

    public static <T> HttpResult<T> success(T data) {
        HttpResult<T> result = new HttpResult<>();
        result.setCode(ErrorCode.CODE_SUCCESS);
        result.setData(data);
        return result;
    }

    public static <T> HttpResult<T> success(T data, PageBean page) {
        HttpResult<T> result = HttpResult.success(data);
        result.setPage(page);
        return result;
    }

    public static <T> HttpResult<T> success(T data, PageBean page, Object extra) {
        HttpResult<T> result = HttpResult.success(data, page);
        result.setExtra(extra);
        return result;
    }

    public static <T> HttpResult<T> procError(String errmsg) {
        return fail(ErrorCode.CODE_PROC_ERROR, errmsg);
    }

    public static <T> HttpResult<T> fail(int errno, String errmsg) {
        HttpResult<T> result = new HttpResult<>();
        result.setCode(errno);
        result.setMessage(errmsg);
        return result;
    }
}

